package com.yy.module.action;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.yy.base.common.action.BaseCtrl;
import com.yy.base.log.Logger;
import com.yy.base.log.LoggerFactory;
import com.yy.base.page.Message;
import com.yy.base.util.ObjectUtil;
import com.yy.base.util.StringUtil;
import com.yy.module.entity.TableJoin;
import com.yy.module.service.TableService;

@Controller
@SuppressWarnings("unchecked")
@RequestMapping(value = "/tableJoinAction")
public class TableJoinAction extends BaseCtrl {
	private Logger log = LoggerFactory.getLogger(TableJoinAction.class);
	private static final long serialVersionUID = 1L;
	@Autowired
	private TableService service;
	/**
	 * 查询表关联信息
	 */
	@ResponseBody
	@RequestMapping(value="/getTableJoinList.html")
	public Message getTableJoinList(HttpServletRequest request) {
		Message message = new Message();
		try {
			String tableName = request.getParameter("tableName");
			message.setPageList(service.getTableJoinList(tableName));
		} catch (Exception e) {
			log.error(e);
			message.setErrorMessage("查询异常！");
		}
		return message;
	}
	
	/**
	 * 保存表信息
	 */
	@ResponseBody
	@RequestMapping(value="/save.html")
	public Message save(HttpServletRequest request) {
		TableJoin join = ObjectUtil.mapToEntity(TableJoin.class, this.getParameterMap(request));
		Message message = new Message();
		try {
			if (StringUtil.isNotEmpty(join.getId())) {
				service.update(join);
			} else {
				service.save(join);
			}
			message.setOkMessage("保存表关联信息成功！");
		} catch (Exception e) {
			log.error(e);
			message.setErrorMessage("保存字段信息异常！");
		}
		return message;
	}
	/**
	 * 删除字段
	 */
	@ResponseBody
	@RequestMapping(value="/deleteJoin.html")
	public Message deleteJoin(HttpServletRequest request) {
		TableJoin join = ObjectUtil.mapToEntity(TableJoin.class, this.getParameterMap(request));
		Message message = new Message();
		try {
			if (StringUtil.isNotEmpty(join.getId())) {
				service.delete(join);
			}
			message.setOkMessage("删除字段信息成功！");
		} catch (Exception e) {
			log.error(e);
			message.setErrorMessage("删除字段信息异常！");
		}
		return message;
	}
}